package com.timmy.google_play.protocol;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.timmy.google_play.base.BaseProtocol;
import com.timmy.google_play.bean.CategoryBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Timmy on 2016/12/13.
 */
public class CategoryProtocol extends BaseProtocol<List<CategoryBean>> {
    @NonNull
    @Override
    protected String getInterfaceKey() {
        return "category";
    }

    @Override
    protected List<CategoryBean> parseData(String body, Gson gson) {

        /**
         * json解析
         */
        try {

            List<CategoryBean> list = new ArrayList<>();

            JSONArray array = new JSONArray(body);

            for (int i = 0; i < array.length(); i++) {

                JSONObject itemJsonObject = array.getJSONObject(i);

                String title = itemJsonObject.getString("title");

                CategoryBean categoryBean = new CategoryBean();

                categoryBean.title = title;
                categoryBean.isTitle = true;

                //加入集合当中
                list.add(categoryBean);

                JSONArray infosJsonArray = itemJsonObject.getJSONArray("infos");

                for (int j = 0; j < infosJsonArray.length(); j++) {

                    JSONObject jsonObject = infosJsonArray.getJSONObject(j);

                    String name1 = jsonObject.getString("name1");
                    String name2 = jsonObject.getString("name2");
                    String name3 = jsonObject.getString("name3");

                    String url1 = jsonObject.getString("url1");
                    String url2 = jsonObject.getString("url2");
                    String url3 = jsonObject.getString("url3");

                    CategoryBean infosCategoryBean = new CategoryBean();

                    infosCategoryBean.name1 = name1;
                    infosCategoryBean.name2 = name2;
                    infosCategoryBean.name3 = name3;

                    infosCategoryBean.url1 = url1;
                    infosCategoryBean.url2 = url2;
                    infosCategoryBean.url3 = url3;

                    list.add(infosCategoryBean);

                }
            }

            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return null;
    }
}
